package pt.um.jspecput.lib.writer;

import static pt.um.jspecput.util.Constants.*;

import java.util.ArrayList;

/**
 * A class that represents java methods representation.
 */
public class Method {

    /** The Constant ARGUMENT_SEPARATOR. */
    private static final String ARGUMENT_SEPARATOR = COMMA + SPACE_CHARACTER;

    /** The method name. */
    private String methodName;

    /** The argument list. */
    private ArrayList<String> argumentList;

    /** The body lines. */
    private ArrayList<String> bodyLines;

    /**
     * Instantiates a new method.
     */
    public Method() {
    }

    /**
     * Instantiates a new method.
     * 
     * @param methodName
     *            the method name
     */
    public Method(String methodName) {
        this.methodName = methodName;
    }

    /**
     * Gets the method name.
     * 
     * @return the method name
     */
    public String getMethodName() {
        return methodName;
    }

    /**
     * Sets the method name.
     * 
     * @param methodName
     *            the new method name
     */
    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    /**
     * Gets the argument list.
     * 
     * @return the argument list
     */
    public ArrayList<String> getArgumentList() {
        return argumentList;
    }

    /**
     * Sets the argument list.
     * 
     * @param argumentList
     *            the new argument list
     */
    public void setArgumentList(ArrayList<String> argumentList) {
        this.argumentList = argumentList;
    }

    /**
     * Gets the body lines.
     * 
     * @return the body lines
     */
    public ArrayList<String> getBodyLines() {
        return bodyLines;
    }

    /**
     * Sets the body lines.
     * 
     * @param bodyLines
     *            the new body lines
     */
    public void setBodyLines(ArrayList<String> bodyLines) {
        this.bodyLines = bodyLines;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = PRIME;
        int result = 1;
        result = prime * result
                + ((argumentList == null) ? 0 : argumentList.hashCode());
        result = prime * result
                + ((bodyLines == null) ? 0 : bodyLines.hashCode());
        result = prime * result
                + ((methodName == null) ? 0 : methodName.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Method other = (Method) obj;
        boolean areEquals = true;
        areEquals &= (argumentList == null ? argumentList == other
                .getArgumentList() : argumentList.equals(other
                .getArgumentList()));
        areEquals &= (bodyLines == null ? bodyLines == other.getBodyLines()
                : bodyLines.equals(other.getBodyLines()));
        areEquals &= (methodName == null ? methodName == other.getMethodName()
                : methodName.equals(other.getMethodName()));
        return areEquals;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("Method: ");

        sb.append(methodName);
        sb.append(OPEN_PARENTHESES);
        if (argumentList != null) {
            int i;
            for (i = 0; i < argumentList.size() - 1; i++) {
                sb.append(argumentList.get(i));
                sb.append(ARGUMENT_SEPARATOR);
            }
            sb.append(argumentList.get(i));
        }
        sb.append(CLOSE_PARENTHESES);

        return sb.toString();
    }
}
